<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <el-form-item prop="username">
          <!-- 用户名 -->
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
            placeholder="用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <!-- 密码 -->
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-lock_fill"
            placeholder="密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <!-- 按钮区域 -->
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" v-on:click="resetLoginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 这是表单的验证规则
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3-10之间', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', triggle: 'blur' },
          { min: 5, message: '密码长度不能小于5位', triggle: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginFrom: function () {
      console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      // 表单预验证 接受一个回调函数来获取表单验证的结果
      this.$refs.loginFormRef.validate(async valid => {
        // valid 是表单验证的结果
        if (!valid) return false
        // const result = await this.$http.post('login', {
        //   ...this.loginForm
        // })
        const { data: result } = await this.$http.post('login', {
          ...this.loginForm
        })
        if (result.meta.status !== 200) {
          return this.$message.error('登录失败')
        }
        this.$message.success('登录成功')
        console.log(result)
        /*  1. 将登录成功后的 token 保存到客户端的 sessionStorafe 中
              1.1 项目中除了登录之外的其它API接口, 必须登录之后才能访问
              1.2 token只应该在当前网页打开期间生效, 所以应该保存在sessionStorage中
            2. 通过编程式导航跳转到后台主页, 路由地址是 /home */
        window.sessionStorage.setItem('token', result.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  position: relative;
  height: 100%;
  background-color: #264a6b;
}

.login_box {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;

  .avatar_box {
    width: 100px;
    height: 100px;
    margin: 0 auto;
    padding: 10px;
    margin-top: -60px;
    border-radius: 50%;
    border: 1px solid #eee;
    background-color: #fff;
    box-shadow: 0 0 6px 0 #ddd;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .login_form {
    box-sizing: border-box;
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
